﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using ZF.WS.UserControls;
using GCELibrary.Current;
using System.Transactions;
using ZF.BLL;
using Newtonsoft.Json;
using ZF.BLL.Admin;
using System.Web.UI.HtmlControls;
namespace ZF.WS.Person
{
    public partial class applye : ZF.BLL.Page.GMPage
    {
        [PageArgument(Key = "ot", DefaultValue = PageOperType.Edit)]
        public override PageOperType OperType
        {
            get
            {
                return base.OperType;
            }
            set
            {
                base.OperType = value;
            }
        }
        [PageArgument(Key = "type", DefaultValue=0)]
        public int Type { get; set; }//0为审请，1为普通
        protected void Page_Load(object sender, EventArgs e)
        {
            
           

            if (!IsPostBack)
            {
                if (this.OperType == PageOperType.AddNew)
                {
                    Add();
                }
                else
                {
                    Edit();
                }

            }
        }
        public void InitControls()
        {
            this.ddlType.DataSource = BLL.DictionaryConfig.EExpertType;
            this.ddlType.DataBind();
 
            var sp = new house.Search();
            this.ddlArea.DataSource = sp.City.Select(p => new { Key = p.Name, Text = p.Name }); ;
            this.ddlArea.DataBind();
            
        }
        public Model.US_Member Member { get; set; }
        public void Add()
        {
            InitControls();
            using (var dc = SiteInfo.CreateDBDataContext())
            {
                var member = dc.US_Member.FirstOrDefault(p => p.MemberId == CurrentUser.MemberId);
                if (member == null)
                    throw WarnFactory.CreateWarn(WarnCode.ReocrdNoFind);
                if (member != null && member.IsExpert)
                {
                  WarnFactory.CreateWarn(new PageWarnParameter()
                  {
                      Link = WarnLink.GoBack,
                      Message = "您已是专家顾问,请勿重复申请",
                      JscriptMode = true,
                  }).Alert(); return;
                }
                this.fckAbout.Value = member.About;
                this.tbSpecialty.Text = member.Specialty;
                this.tbAddress.Text = member.Address;
                this.tbContact.Text = member.Contact;
                this.tbIDCard.Text = member.IDCard;
                this.tbPhone.Text = member.Phone;
                this.tbTel.Text = member.Tel;
                this.tbCompany.Text = member.Company;
                this.regUpdateImg1.tbBusImg.Text = member.ImgBusCard;
                this.regUpdateImg1.tbIDCardImg.Text = member.ImgIDCard;
                this.regUpdateImg1.tbLicImg.Text = member.ImgLicence;
                this.tbInte.Text = member.InteSet.ToString();
                this.ddlJob.SelectedValue = member.Professional;
                this.tbQQ.Text = member.QQ;
                this.rblSex.SelectedValue = member.Sex.ToString();
                this.tbZipcode.Text = member.Zipcode;
                this.tbBD.Text = member.Birthday;
                this.tbCompany.Text = member.Company;
                this.tbSpecialty.Text = member.Specialty;
                this.tbPhoto.Text = member.Icon;
                this.tbBlog.Text = member.Blog;
                this.lvImgList.DataSource = member.Icon.EmptyString("").Split(new string[] { ";" }, StringSplitOptions.RemoveEmptyEntries)
                    .Select(p => new
                    {
                        File = p
                    });
                this.lvImgList.DataBind();
            }
        }
        public void AddSubmit(object sender, EventArgs e)
        {
            using (var dc = SiteInfo.CreateDBDataContext())
            {
                var member = dc.US_Member.FirstOrDefault(p => p.MemberId == CurrentUser.MemberId);
                if (member == null)
                    throw WarnFactory.CreateWarn(WarnCode.ReocrdNoFind);
                member.Status = 1;
                member.Type = 1;
                member.About = this.fckAbout.Value;
                member.Specialty = this.tbSpecialty.Text;
                member.ExpertType = this.ddlType.SelectedValue.Convert<int>();
                member.Professional = this.ddlJob.SelectedValue.EmptyString();
                member.Tel = this.tbTel.Text.EmptyString();
                member.Area = this.ddlArea.SelectedValue;
                member.Company = this.tbCompany.Text.EmptyString();
                member.ImgBusCard = this.regUpdateImg1.tbBusImg.Text.EmptyString();
                member.ImgIDCard = this.regUpdateImg1.tbIDCardImg.Text.EmptyString();
                member.ImgLicence = this.regUpdateImg1.tbLicImg.Text.EmptyString();
                member.InteSet = this.tbInte.Text.Convert<int>();
                if (!string.IsNullOrEmpty(this.tbBlog.Text))
                    member.Blog = null;
                else
                    member.Blog = "http://" + this.tbBlog.Text.ToLower().Replace("http://", "");
                member.Address = this.tbAddress.Text.EmptyString();
                member.Contact = this.tbContact.Text.EmptyString();
                member.IDCard = this.tbIDCard.Text.EmptyString();
                member.Phone = this.tbPhone.Text.EmptyString();
                member.Professional = this.ddlJob.SelectedValue.EmptyString();
                member.QQ = this.tbQQ.Text.EmptyString();
                member.Sex = this.rblSex.SelectedValue.Convert<int>();
                member.Tel = this.tbTel.Text.EmptyString();
                member.Zipcode = this.tbZipcode.Text.EmptyString();
                member.Birthday = this.tbBD.Text;
                member.Company = this.tbCompany.Text.EmptyString();
                member.Specialty = this.tbSpecialty.Text;
                var photoList = this.tbPhoto.Text.Split(new string[] { ";" }, StringSplitOptions.RemoveEmptyEntries).ToList();
                photoList.AddRange(this.SWFUpLoad1.UpLoadFileInfos);
                member.Icon = photoList.ConstituteString(";").Split(new string[] { ";" }, StringSplitOptions.RemoveEmptyEntries)
                    .ConstituteString(";");
                var ea = new ZF.Model.E_ExpertApply()
                {
                    AddTime = DateTime.Now,
                    Content = this.tbReason.Text,
                    Status = 0,
                    MemberId = CurrentUser.MemberId,
                };
                try
                {
                    if(Type!=1)
                    dc.E_ExpertApply.InsertOnSubmit(ea);
                    dc.SubmitChanges();
                }
                catch (Exception ex)
                {
                    throw WarnFactory.CreateWarn(ex);
                }

                WarnFactory.CreateWarn(new PageWarnParameter()
                {
                    DoPage = "/Person/index.aspx",
                     JscriptMode=true,
                    Message = "申请提交成功，正在等待审核",
                }).Alert();
                return;
                string msgKey = "";
                WarnMessageLink link = new WarnMessageLink(Type==1?"操作成功":"申请提交成功，正在等待审核");
                WarnFactory.CreateWarn(link.ToString(), WarnIcon.Success, "/Person/index.aspx").Alert();

            }

        }

        public void Edit()
        {
            using (var dc = BLL.SiteInfo.CreateDBDataContext())
            {

                var member = dc.US_Member.FirstOrDefault(p => p.MemberId == this.CurrentUser.MemberId);
                if (member == null)
                    throw WarnFactory.CreateWarn(WarnCode.ReocrdNoFind);
                InitControls();
                Member = member;
                if(member.IsExpert)
                    this.ddlType.SelectedValue = member.ExpertType.ToString();
                this.fckAbout.Value = member.About;
                this.tbSpecialty.Text = member.Specialty;
                this.tbAddress.Text = member.Address;
                this.tbContact.Text = member.Contact;
                this.tbIDCard.Text = member.IDCard;
                this.tbPhone.Text = member.Phone;
                this.tbTel.Text = member.Tel;
                this.tbCompany.Text = member.Company;
                this.regUpdateImg1.tbBusImg.Text = member.ImgBusCard;
                this.regUpdateImg1.tbIDCardImg.Text = member.ImgIDCard;
                this.regUpdateImg1.tbLicImg.Text = member.ImgLicence;
                this.tbInte.Text = member.InteSet.ToString();
                this.ddlJob.SelectedValue = member.Professional;
                this.tbQQ.Text = member.QQ;
                this.rblSex.SelectedValue = member.Sex.ToString();
                this.tbZipcode.Text = member.Zipcode;
                this.tbBD.Text = member.Birthday;
                this.tbCompany.Text = member.Company;
                this.tbSpecialty.Text = member.Specialty;
                this.tbPhoto.Text = member.Icon;
                this.tbBlog.Text = member.Blog;
                this.lvImgList.DataSource = member.Icon.EmptyString("").Split(new string[] { ";" }, StringSplitOptions.RemoveEmptyEntries)
                    .Select(p => new
                    {
                        File = p
                    });
                this.lvImgList.DataBind();

                if (!member.IsExpert)
                {
                    foreach (Control c in ((Person)this.Page.Master).ContentPlaceHolder1.Controls)
                    {
                        if (c is HtmlControl)
                        {
                            var hc = (HtmlControl)c;
                            if (!string.IsNullOrEmpty(hc.Attributes["key"]))
                            {
                                hc.Visible = !hc.Attributes["key"].EqIgnoreCase("zj");
                            }
                        }
                    }
                }


            }
        }
        public void EditSubmit(object sender, EventArgs e)
        {

            using (var ts = new System.Transactions.TransactionScope())
            {
                using (var dc = BLL.SiteInfo.CreateDBDataContext())
                {
                    var member = dc.US_Member.FirstOrDefault(p => p.MemberId == this.CurrentUser.MemberId);
                    if (member == null)
                        throw WarnFactory.CreateWarn(WarnCode.ReocrdNoFind);




                    member.About = this.fckAbout.Value;
                    member.Specialty = this.tbSpecialty.Text;
                    member.ExpertType = this.ddlType.SelectedValue.Convert<int>();
                    member.Professional = this.ddlJob.SelectedValue.EmptyString();
                    member.Tel = this.tbTel.Text.EmptyString();
                    member.Area = this.ddlArea.SelectedValue;
                    member.Company = this.tbCompany.Text.EmptyString();
                    member.ImgBusCard = this.regUpdateImg1.tbBusImg.Text.EmptyString();
                    member.ImgIDCard = this.regUpdateImg1.tbIDCardImg.Text.EmptyString();
                    member.ImgLicence = this.regUpdateImg1.tbLicImg.Text.EmptyString();
                    member.InteSet = this.tbInte.Text.Convert<int>();
                    if (string.IsNullOrEmpty(this.tbBlog.Text))
                        member.Blog = null;
                    else
                        member.Blog = "http://" + this.tbBlog.Text.ToLower().Replace("http://", "");
                    member.Address = this.tbAddress.Text.EmptyString();
                    member.Contact = this.tbContact.Text.EmptyString();
                    member.IDCard = this.tbIDCard.Text.EmptyString();
                    member.Phone = this.tbPhone.Text.EmptyString();
                    member.Professional = this.ddlJob.SelectedValue.EmptyString();
                    member.QQ = this.tbQQ.Text.EmptyString();
                    member.Sex = this.rblSex.SelectedValue.Convert<int>();
                    member.Tel = this.tbTel.Text.EmptyString();
                    member.Zipcode = this.tbZipcode.Text.EmptyString();
                    member.Birthday = this.tbBD.Text;
                    member.Company = this.tbCompany.Text.EmptyString();
                    member.Specialty = this.tbSpecialty.Text;
                    var photoList = this.tbPhoto.Text.Split(new string[] { ";" }, StringSplitOptions.RemoveEmptyEntries).ToList();
                    photoList.AddRange(this.SWFUpLoad1.UpLoadFileInfos);
                    member.Icon = photoList.ConstituteString(";").Split(new string[] { ";" }, StringSplitOptions.RemoveEmptyEntries)
                        .ConstituteString(";");


                    try
                    {
                        dc.SubmitChanges();
                        ts.Complete();
                    }
                    catch (Exception ex)
                    {
                        throw WarnFactory.CreateWarn(ex);
                    }
                    WarnFactory.CreateWarn(new PageWarnParameter()
                    {
                        Link = WarnLink.Current,
                        JscriptMode = true,
                        Message = "修改成功",
                    }).Alert();
                    return;


                    string msgKey = "个人资料";

                    SiteInfo.InsertLog("修改" + "个人信息", null);
                    WarnMessageLink link = new WarnMessageLink(string.Format("成功修改{0}", msgKey));

                    link.AddWarnMessageLink(string.Format("重新修改{0}", msgKey), Request.Url.ToString());
                    WarnFactory.CreateWarn(link.ToString(), WarnIcon.Success, "/Person/index.aspx").Alert();
                }


            }

        }





    }
}
